package com.five.community.controller;

import com.five.community.bean.Activity;
import com.five.community.bean.Mailbox;
import com.five.community.bean.Repair;
import com.five.community.bean.Village;
import com.five.community.bean.vo.ActivityVo;
import com.five.community.bean.vo.ComplaintVo;
import com.five.community.bean.vo.MailboxVo;
import com.five.community.contant.AppContants;
import com.five.community.service.MailboxService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/mailbox")
public class MailboxController {

    @Autowired
    private MailboxService mailboxService;

    @Autowired
    private VillageService villageService;
    //信箱一览页面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map,Integer pageNum,String name){
        if (pageNum == null) {
            pageNum = 1;
        }

        if(name==null||name=="") {
            try {
                PageInfo<MailboxVo> pageInfo = mailboxService.getPageInfo(pageNum, AppContants.pageSize);
                map.addAttribute("pageInfo",pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else {try {
            PageHelper.startPage(pageNum,AppContants.pageSize);
            List<MailboxVo> mailboxVos = mailboxService.selectByName(name);
            PageInfo<MailboxVo> pageInfo = new PageInfo<>(mailboxVos);
            map.addAttribute("pageInfo", pageInfo);
            Mailbox mailbox = new Mailbox();
            mailbox.setName(name);
            map.addAttribute("mailbox",mailbox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
        return "mailboxlist";
    }
    //信箱搜索根据名字
//    @RequestMapping("/selectUi")
//    public String selectUi(ModelMap map,String name,Integer pageNum){
//        if (name=="") {
//            return "redirect:/mailbox/listUi";
//        } else {
//            try {
//
//                if (pageNum == null) {
//                    pageNum = 1;
//                }
//                    PageHelper.startPage(pageNum,AppContants.pageSize);
//                    List<MailboxVo> mailboxVos = mailboxService.selectByName(name);
//                    PageInfo<MailboxVo> pageInfo = new PageInfo<>(mailboxVos);
//                    map.addAttribute("pageInfo", pageInfo);
//                } catch (Exception e) {
//                    e.printStackTrace();
//             }
//        }
//        return "mailboxlist";
//    }
    //信箱信息删除
    @RequestMapping("/limit/delete")
    public String delete(long id){
        try {
            mailboxService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/mailbox/listUi";
    }
    //添加页面
    @RequestMapping("/limit/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages", villages);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "mailbox_add";
    }
    //添加信箱
    @RequestMapping("/add")
    public String add(MailboxVo mailboxVo){
        try {
            long vid = Long.parseLong(mailboxVo.getVname());
            Mailbox mailbox = new Mailbox(vid,mailboxVo.getName(),mailboxVo.getLetterName(),new Date(), mailboxVo.isState());
            mailboxService.insert(mailbox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/mailbox/addUi";
    }
    //修改编辑页面
    @RequestMapping("/limit/editUi")
    public String editUi(long id, ModelMap map){
        try {
            Mailbox mailbox = mailboxService.selectById(id);
            map.addAttribute("mailbox",mailbox);
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages",villages);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "mailbox_edit";
    }
    //修改编辑信箱
    @RequestMapping("/edit")
    public String edit(MailboxVo mailboxVo){
        try {
            long vid = Long.parseLong(mailboxVo.getVname());
            Mailbox mailbox = new Mailbox(mailboxVo.getId(),vid,mailboxVo.getName(),mailboxVo.getLetterName(),mailboxVo.getCreateDate(), mailboxVo.isState());
            mailboxService.update(mailbox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "mailbox_edit";
    }
}
